package com.example.smartagriculture.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.smartagriculture.controller.DTO.AllEnvironmentalInformationDTO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.smartagriculture.utils.Result;
import com.example.smartagriculture.service.IAllEnvironmentalInformationService;
import com.example.smartagriculture.entity.AllEnvironmentalInformation;
import io.swagger.v3.oas.annotations.media.Schema;
import java.io.Serial;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

/**
 * AllEnvironmentalInformationController 前端控制器
 * @author 张芸博
 * @since 2024-12-28
 */
@RestController
@RequestMapping("/all-environmental-information")
public class AllEnvironmentalInformationController {

    @Resource
    private IAllEnvironmentalInformationService allEnvironmentalInformationService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    @Operation(description = "新增或更新")
    public Result save(@RequestBody AllEnvironmentalInformation allEnvironmentalInformation) {
        return Result.success(allEnvironmentalInformationService.saveOrUpdate(allEnvironmentalInformation));
    }

    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    @Operation(description = "通过id删除")
    public Result delete(@PathVariable Integer id) {
        AllEnvironmentalInformation allEnvironmentalInformation =allEnvironmentalInformationService.getById(id);
        return Result.success(allEnvironmentalInformationService.saveOrUpdate(allEnvironmentalInformation));
    }

    /**
    *  查询所有
    */
    @GetMapping("/all")
    @Operation(description = "查询所有")
    public Result findAll() {
        QueryWrapper<AllEnvironmentalInformation> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("is_delete", "0");
       List<AllEnvironmentalInformation> list=  allEnvironmentalInformationService.list(queryWrapper);
        return Result.success(list);
    }
    /**
    *  根据id查询
    */
    @GetMapping("/{id}")
    @Operation(description = "根据id查询")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(allEnvironmentalInformationService.getById(id));
    }
    /**
    *  分页查询
    */
    @GetMapping("/page")
    @Operation(description = "分页查询")
    public Result findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        QueryWrapper<AllEnvironmentalInformation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        queryWrapper.orderByDesc("id");
        return Result.success(allEnvironmentalInformationService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    /**
     *  查询最新十条数据取平均数
     */
    @GetMapping("/getTen")
    @Operation(description = "获取最新十条机器数据的平均数")
    public Result getTen(){
    return Result.success(allEnvironmentalInformationService.getTen());
    }

    @GetMapping("/getAvg")
    @Operation(description = "获取年、月、日的平均数-折线图数据")
    public Result getAvg(String type){
        if("year".equals(type)){
            List<AllEnvironmentalInformationDTO> list = allEnvironmentalInformationService.getYearAvg();
        }else if("month".equals(type)){
            List<AllEnvironmentalInformationDTO> list = allEnvironmentalInformationService.getMonthAvg();
            System.out.println(list);
        }else if("day".equals(type)){
            List<AllEnvironmentalInformationDTO> list = allEnvironmentalInformationService.getDayAvg();
        }
        return null;
    }
}

